﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Controllers.Admin;
using WebHoiChoMuaBan.Controllers.Global;
using WebHoiChoMuaBan.Models;

namespace WebHoiChoMuaBan.Controllers.Admin
{
    public class AdminProductController : AdminController
    {
        public ActionResult Index(String typeNews)
        {
            //Mặc định là phần đăng sản phẩm trước
            if (String.IsNullOrEmpty(typeNews))
                typeNews = "Product";
            if (typeNews.Equals("Product"))
            {
                CategoryHelper cateHelper = new CategoryHelper();
                ViewData["Categories"] = cateHelper.GetListParentCategory();
            }
            else //RealEstate
            {

            }

            UtilityModel uModel = new UtilityModel();
            ViewData["Provinces"] = uModel.ListProvince();
            return View("~/Views/Admin/PostProduct.aspx");
        }

        public ActionResult ListProduct(int? cateId)
        {
            if (IsLogin && checkRight())
            {
                if ((bool)ViewData["IsOwner"])
                {
                    ViewData["CateId"] = cateId;
                    int pageNum;
                    if (!int.TryParse(Request.QueryString["p"], out pageNum))
                        pageNum = 1;
                    else
                        pageNum = int.Parse(Request.QueryString["p"]);
                    NewsProductHelper newsProductHelper = new NewsProductHelper();
                    if (cateId == null)
                    {
                        CategoryHelper cateHelper = new CategoryHelper();
                        cateId = cateHelper.getRandomCategory();
                    }
                    ViewData["ListNewsProduct"] = newsProductHelper.AllNewsProductByCategory(pageNum, Config.ITEMS_PER_ADMINPAGE, cateId.Value);
                    return View("~/Views/Admin/Product_List.aspx");
                }
            }
            return HomeAdminPage();
        }

        [HttpGet]
        public ActionResult Revising(int id)
        {
            NewsProductHelper proHelper = new NewsProductHelper();
            ViewData["NewsProduct"] = proHelper.getNewsProductById(id);
            return View("~/Views/Admin/Revising.aspx");
        }

        [HttpGet]
        public ActionResult DeleteProduct(int productId)
        {
            NewsProductHelper prodHelper = new NewsProductHelper();
            NewsProduct prod = prodHelper.getNewsProductById(productId);
            int cateId = prod.CateId;
            prodHelper.Delete(prod);
            prodHelper.Save();
            return ListProduct(cateId);
        }
        protected void LoadTimeAndCurrency()
        {
            UtilityModel mTime = new UtilityModel();
            string currency = mTime.time + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tỷ giá: " + mTime.exchange_rate;
            ViewData["Currency"] = currency;
        }
        private void Init(int? uid)
        {
            ViewData["mid"] = "product";
            ViewData["child_menu"] = "posting";
            LoadTimeAndCurrency();
            //Load tất cả các danh mục
            CategoryHelper cateHelper = new CategoryHelper();
            // ViewData["Categories"] = new SelectList(cateHelper.GetListParentCategory(), "CateId", "CateName");
            ViewData["Categories"] = new SelectList(cateHelper.getMemberCategory(uid), "CateId", "CateName");
            UtilityModel uModel = new UtilityModel();
            ViewData["Provinces"] = uModel.ListProvince();
        }

        [HttpGet]
        public ActionResult EditProduct(int? uid, int productId)
        {
            if (!this.IsLogin || uid == null)
                return HomeAdminPage();
            var result = LoadMember(uid.Value);
            if (result == null)
            {
                //Kiểm tra có phải là chủ nhân, nếu đúng thì cho sửa, không thì bắt login
                if (ViewData["IsOwner"] == null)
                    return HomeAdminPage();
                if ((bool)ViewData["IsOwner"] == false)
                    return HomeAdminPage();
                Init(uid);
                ViewData["child_menu"] = "editing";
                NewsProductHelper productHelper = new NewsProductHelper();
                //Load và delete product
                NewsProduct product = productHelper.getNewsProductById(productId);
                if (product.RAttrId == null)  //Nếu sản phẩm đang chỉnh sửa là product
                    ViewData["NewsType"] = "Product";
                else
                    ViewData["NewsType"] = "RealEstate";
                ViewData["ProductToEdit"] = product;

                UtilityModel uModel = new UtilityModel();
                SelectList listProvinces = new SelectList(uModel.ListProvince(), "ProvId", "ProvName", product.Province.ProvId);
                ViewData["Provinces"] = listProvinces;

                CategoryHelper cateHelper = new CategoryHelper();
                //SelectList listCate = new SelectList(cateHelper.getAllParentCate(), "CateId", "CateName", product.ParentCateId);
                //ViewData["Categories"] = listCate;
                //ViewData["CateLevel1"] = product.ParentCateId;
                ViewData["CateLevel2"] = product.CateId;
                ViewData["ProductToEdit"] = productHelper.getNewsProductById(productId);
                return View("~/Views/Admin/EditProduct.aspx");
            }
            return result;
        }

        [HttpPost]
        public ActionResult Controller(FormCollection data)
        {
            NewsProductHelper productHelper = new NewsProductHelper();
            int curPage = int.Parse(data.Get("curPage"));
            String[] lstProductId = data.GetValues("checkbox");
            //Click "xoa" button
            if (data.Get("hidCmd").Equals("del"))
            {
                if (lstProductId != null)
                {
                    foreach (string item in lstProductId)
                    {
                        NewsProduct product = productHelper.getNewsProductById(int.Parse(item));
                        productHelper.Delete(product);
                        productHelper.Save();
                    }
                }
            }
            //Click "chuyen trang thai" button
            if (data.Get("hidCmd").Equals("ChangeState"))
            {
                if (lstProductId != null)
                {
                    foreach (string item in lstProductId)
                    {
                        NewsProduct product = productHelper.getNewsProductById(int.Parse(item));
                        if (product != null)
                        {
                            if (product.NProState)
                                product.NProState = false;
                            else
                                product.NProState = true;
                            productHelper.Save();
                        }
                    }
                }
            }
            return RedirectToAction("ListProduct", "AdminProduct", new { p = curPage });
        }

        [HttpPost]
        public RedirectResult ChangeProductState(string id)
        {
            new NewsProductHelper().changeNewsProductState(id);
            return Redirect(Request.UrlReferrer.AbsoluteUri);

        }
    }
}
